import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import panosRoutes from './modules/panos'
import mapRoutes from './modules/map'

// 基础路由列表
export const viewsRoutesList = [
  {
    path: '/',
    name: 'home',
    redirect: {name: 'Panos'},
    meta: {title: ''}
  },
  panosRoutes,
  mapRoutes,
]
// 静态路由
export const staticRoutesLists = [
  ...viewsRoutesList,
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/common/error-page/404.vue'),
    meta: {title: '404未找到'}
  }
]

/**
 * 测试路由，不打包到生产环境中
 * @type {{path: string, component: (function(): *), meta: {title: string}, name: string}[]}
 */
const testRoutesList = []

const createRouter = () => new VueRouter({
  mode: 'hash',
  scrollBehavior: () => ({y: 0}),
  routes: [
    ...staticRoutesLists,
    // 测试路由*开发环境
    ...(process.env.NODE_ENV === 'development' ? testRoutesList : []),
    // 任意路由*重定向到404页面
    {path: '/:pathMatch(.*)*', redirect: {name: '404'}}
  ]
})

const router = createRouter()

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

/**
 * 重置路由*退出登录 删除动态添加的路由
 *
 */
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
